package cz.muni.fi.pa165.library.web_layer.adminPages.reservation;

import cz.muni.fi.pa165.library.web_layer.adminPages.Reader.ShowAllReader;
import cz.muni.fi.pa165.library.api.BookService;
import cz.muni.fi.pa165.library.api.BookTO;
import cz.muni.fi.pa165.library.api.ReaderService;
import cz.muni.fi.pa165.library.api.ReservationService;
import cz.muni.fi.pa165.library.api.ReservationTO;
import cz.muni.fi.pa165.library.web_layer.ApplicationContextProvider;
import cz.muni.fi.pa165.library.web_layer.Template;
import java.sql.Timestamp;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

/**
 *
 * @author dulinka
 */
public class EditReservation extends Template {

    private static final long serialVersionUID = 1L;
    private ReservationTO reservation;
    private StringValue readerId;

    public EditReservation(final PageParameters parameters) {
        super(parameters);
        ReaderService readerService = (ReaderService) ApplicationContextProvider.getApplicationContext().getBean("readerService");
        BookService bookService = (BookService) ApplicationContextProvider.getApplicationContext().getBean("bookService");
        readerId = parameters.get("readerId");
        reservation = new ReservationTO();
        add(new Label("title", new Model("Create reservation for "
                + readerService.findReaderById(readerId.toLong()).getSurname() + " "
                + readerService.findReaderById(readerId.toLong()).getFirstName())));

        Form<?> form = new Form("form") {
            @Override
            protected void onSubmit() {
                ReservationService reservationService = (ReservationService) ApplicationContextProvider.getApplicationContext().getBean("reservationService");
                ReaderService readerService = (ReaderService) ApplicationContextProvider.getApplicationContext().getBean("readerService");
                java.util.Date date = new java.util.Date();
                reservation.setReservationDate(new Timestamp(date.getTime()));
                reservation.setReaderTO(readerService.findReaderById(readerId.toLong()));
                reservationService.insertReservation(reservation);
                setResponsePage(ShowAllReservation.class);
            }
        };
        add(form);
        List<BookTO> books = bookService.findAllAvailableBooks();
        DropDownChoice choiceState = new DropDownChoice("bookTO", new PropertyModel(reservation, "bookTO"), books);
        form.add(choiceState);

        BookmarkablePageLink homeLink = new BookmarkablePageLink<>(
                "list", ShowAllReader.class);
        add(homeLink);
    }
}